using System;
using UnityEngine;
using UnityEngine.UI;

public class LogTool : MonoBehaviour
{
    //// <summary>
    /// 日志输入框
    /// </summary>
    private static InputField Log;

    void Start()
    {
        Log = transform.Find("Viewport/Content/Log").GetComponent<InputField>();
    }

    /// <summary>
    /// 输出日志
    /// </summary>
    /// <param name="info"></param>
    public static void WriteLog(string info)
    {
        ConsoleRow();
        Log.text = $"<color=green>提示：{info}</color>\n" + Log.text;
    }
    /// <summary>
    /// 输出报错日志
    /// </summary>
    /// <param name="info"></param>
    public static void WriteError(string info)
    {
        ConsoleRow();
        Log.text = $"<color=red>错误：{info}</color>\n" + Log.text;
    }

    /// <summary>
    /// 控制行数
    /// </summary>
    private static void ConsoleRow()
    {
        if (Log.text.Length > 6000)
        {
            string[] rows = Log.text.Split("\n");
            int len = Math.Min(rows.Length, 100);
            Log.text = "";
            for (int i = 0; i < len; i++)
            {
                Log.text += rows[i] + "\n";
            }
            if(!Log.text.EndsWith("</color>"))Log.text += "</color>";
        }
    }
}
